package com.zixi.beverage_order.config;

import com.zixi.beverage_order.pojo.Seller;
import com.zixi.beverage_order.service.seller.SignUpInService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    SignUpInService signUpInService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("执行了授权！");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行了认证！");

        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;

        Seller seller = signUpInService.shiroLogin(usernamePasswordToken.getUsername());

        if (seller==null){
            return null;
        }else {
            SecurityUtils.getSubject().getSession().setAttribute("username",seller.getUsername());//用户名给session
            SecurityUtils.getSubject().getSession().setAttribute("phonenumber",seller.getPhonenumber());//id给session
            return new SimpleAuthenticationInfo(seller,seller.getPassword(),"");
        }

    }
}
